<?php
/**
 * Manages rules for an item
 * 
 */
class CompanyController extends Controller {

    private function getNewsList($companyName){
        $companyNews = $this->model->searchCompanyNews($companyName);
        return $companyNews;
    }

    /*
     * Browse for items
     */
    public function browse(){
        $listCompanyPage = $this->model->listCompanyPage(1);

        $this->set('listCompanyPage', $listCompanyPage);
        $this->view->setPageTitle('Lista företagssidor');
        $this->view->noSub();
    }

    
    /*
     * Show a single item
     */
    public function show(){

    
        $id = $this->query[0];

         //get first parameter of query array
        $id = $this->query[0];

        // check so that id is an integer
        if(is_numeric($id)){
            $companyPage = $this->model->readCompanyPage($id);
            $companyName = $companyPage[0]['name'];
            
            // ads
            $companyAds = $this->model->searchCompanyAds($companyName);
            // news
            $companyNews = $this->getNewsList($companyName);

            // set
            $this->set('companyNews', $companyNews);
            $this->set('companyPage', $companyPage[0]);
            $this->set('companyAds', $companyText);

            // set page properties
            $this->view->setPageTitle($companyName);
            $this->view->addCss('subpage');

        }
        else {
            return false;
        }
       
    }



    /*
     * Show offers for the company
     */
    public function offers(){
        // get first parameter of query array
        $id = $this->query[0];

        // check so that id is an integer
        if(is_numeric($id)){
            $companyPage = $this->model->readCompanyPage($id);
            $companyName = $companyPage[0]['company'];

            // offers
            $companyPageOffer = $this->model->readCompanyPageOffer($companyPage[0]['id']);

            // news
            $companyNews = $this->getNewsList($companyName);

            // set
            $this->set('companyNews', $companyNews);
            $this->set('companyPage', $companyPage[0]);
            $this->set('companyPageOffer', $companyPageOffer);
            $this->view->setPageTitle($companyPageName.' - Erbjudanden');
            $this->view->addCss('subpage');
        }
        else {
            return false;
        }
    }


    /*
     * Search and show news of the company
     */
    public function news(){
        // get first parameter of query array
        $id = $this->query[0];

        // check so that id is an integer
        if(is_numeric($id)){
            $companyPage = $this->model->readCompanyPage($id);
            $companyNews = $this->model->searchCompanyNews($companyPage[0]['company']);

            $this->set('companyPage', $companyPage[0]);
            $this->set('companyNews', $companyNews);
            $this->view->setPageTitle($companyPage[0]['company'].' - Nyheter');
            $this->view->addCss('subpage');

        }
        else {
            return false;
        }
    }


    /*
     * Search and show videos for the company
     */
    public function videos(){
        // get first parameter of query array
        $id = $this->query[0];

        // check so that id is an integer
        if(is_numeric($id)){
            $companyPage = $this->model->readCompanyPage($id);
            $companyName = $companyPage[0]['company'];
            // videos
            $companyVideos = $this->model->searchCompanyVideos($companyName);
            // news
            $companyNews = $this->getNewsList($companyName);

            // set
            $this->set('companyNews', $companyNews);
            $this->set('companyPage', $companyPage[0]);
            $this->set('companyVideos', $companyVideos);
            $this->view->setPageTitle($companyName.' - Videos');
            $this->view->addCss('subpage');

        }
        else {
            return false;
        }
    }

}
?>
